// pages/shoppingMall/categoryGoods/categoryGoods.js
Page({
  /**
   * 页面的初始数据
   */
  pusher:null,
  data:{
    beautyValue:0,
    whitenessValue:0,
    cameraPosition:'前置摄像头',
    state:{
      status:true,
      showStatus:'暂停',
      lock:true
    },
    switch:{
      status: false,
      showStatus: '开始'
    }
  },
  changeLens:function() {
    var that=this;
    this.pusher.switchCamera({
      success: function () {
        console.log('switch success!');
        if (that.data.cameraPosition !='前置摄像头'){
            that.setData({
              cameraPosition: "前置摄像头"
            })
        }else{
          that.setData({
            cameraPosition: "后置摄像头"
          })
        }
      },
        fail: function () {
          console.log('switch failed!')
      },
        complete: function () {
          console.log('switch complete!')
      }
    });
  },
  statechange(e) {
    console.log('live-pusher code:', e.errCode)
  },
  slider1change: function (e){
    this.setData({beautyValue:e.detail.value});
  },
  slider2change: function (e) {
    this.setData({whitenessValue: e.detail.value });
  },
  startOrpause:function(){
    var that=this;
    if (this.data.state.status==true){
      this.pusher.pause({
        success: function () {
          console.log('pause success!');
          that.setData({ 
            state: { 
              status: false, 
              showStatus:"继续"
             }
          });
        },
        fail: function () {
          console.log('pause failed!');
        },
        complete: function () {
          console.log('pause complete!');
        }
      });
    }else{
      this.pusher.resume({
        success: function () {
          console.log('start success!');
          that.setData({
            state: {
              status: true,
              showStatus: "暂停"
            }
          });
        },
        fail: function () {
          console.log('start failed!');
        },
        complete: function () {
          console.log('start complete!');
        }
      });
    }
  },
  endPush:function(){
    var that=this;
    if (this.data.switch.status == true){
      this.pusher.stop({
        success: function () {
          console.log('stop success!');
          that.setData({
            switch: {
              status: false,
              showStatus: "开始"
            },
            state: {
              status: true,
              showStatus: "暂停",
              lock:true
            }
          });
        },
        fail: function () {
          console.log('stop failed!');
        },
        complete: function () {
          console.log('stop complete!');
        }
      });
    }else{
      this.pusher.start({
        success: function () {
          console.log('start success!');
          that.setData({
            switch: {
              status: true,
              showStatus: "结束"
            },
            state: {
              status: true,
              showStatus: "暂停",
              lock:false
            }
          });
        },
        fail: function () {
          console.log('start failed!');
        },
        complete: function () {
          console.log('start complete!');
        }
      });
    }
    console.log(this.data.state.lock);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.camera','scope.record']) {
          wx.authorize({
            scope:'scope.camera',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              wx.createLivePusherContext()
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.pusher=wx.createLivePusherContext('pusher');
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})